<template>
  <div class="movies">
    <div
      class="item"
      v-for="(item, index) in moviesList"
      :key="index"
      @click="gotoDetail(item.movieid)"
    >
      <img :src="item.img | formatUrl" alt="" />
      <p>{{ item.nm }}</p>
    </div>
  </div>
</template>

<script>
import { moviesApi, moviesMoreApi } from "@/utils/api";
export default {
  data() {
    return {
      // 整个的电影列表
      moviesList: [],
      // 放所有的电影id
      ids: [],
      // 所有电影的条数
      count: 0,
      startIndex: 0,
      size: 6,
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await moviesApi();
      this.moviesList = res.result;
      this.ids = res.ids;
      this.count = res.count;
      this.startIndex = res.result.length;

      await this.$nextTick();
      this.$emit("finish");
    },
    async getMore() {
      // arr表示截取的电影id的数组
      const arr = this.ids.slice(this.startIndex, this.startIndex + this.size);
      const str = arr.join(",");
      const res = await moviesMoreApi({
        ids: str,
      });
      // console.log(res);
      this.moviesList = [...this.moviesList, ...res.result];
      this.startIndex += this.size;

      await this.$nextTick();
      this.$emit("more", this.moviesList.length >= this.count);
    },
    gotoDetail(id) {
      this.$router.push("/detail/" + id);
    },
  },
  filters: {
    formatUrl(val) {
      return val.split("/w.h").join("/50.80");
    },
  },
};
</script>

<style lang="less" scoped>
.movies {
  margin-top: 10px;
  background: #fff;

  .item {
    width: 100%;
    height: 114px;
    padding-left: 15px;
    border-bottom: 1px solid #000;
  }
}
</style>
